<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            width: 600px;
            margin: 100px auto;
        }

        .message {
            display: inline-block;
            font-size: 12px;
            color: #999;
            background: url(images/mess.png) no-repeat left center;
            padding-left: 20px;
        }

        .wrong {
            color: red;
            background-image: url(images/wrong.png);
        }

        .right {
            color: green;
            background-image: url(images/right.png);
        }
    </style>
</head>

<body>
    <div class="register">
        <input type="password" class="ipt">
        <p class="message">请输入4~8位密码</p>
    </div>
    <script>
        // 需求：表单失去焦点，如果密码不合法，则 p 标签类样式更改，内容也相应变化
        //      如何合法，也进行相应变化
        let input = document.querySelector('input');
        let p = document.querySelector('p');
        input.onblur = function () {
            let value = input.value;
            if (value.length >= 4 && value.length <= 8) {
                p.className = 'message right';
                p.innerHTML = "密码合法";
            } else {
                p.className = 'message wrong';
                p.innerHTML = "密码不合法";
            }
        }
    </script>
</body>

</html>